package controller;

import com.hazelcast.security.UsernamePasswordCredentials;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import util.Result;

@RestController
public class UserServlet {


    @RequestMapping("/doLogin")
    public void login(String username,String password,boolean rememberMe){
        System.out.println(username+"username");
        System.out.println(password+"passwordy");



        try{
            Subject subject = SecurityUtils.getSubject();

          //  password = new Md5Hash(password,username,1024).toString();

            System.out.println(password);

            UsernamePasswordToken token = new UsernamePasswordToken(username,password);
            if(rememberMe){
                token.setRememberMe(rememberMe);
            }
            subject.login(token);
            System.out.println(subject.isPermitted("sys:user:add"));
            //测试是否获取角色
            System.out.println(subject.hasRole("开发工程师"));



        }catch (UnknownAccountException e){
            e.printStackTrace();
            System.out.println(Result.error("用户名不存在"));
        } catch (IncorrectCredentialsException e){
            e.printStackTrace();
            System.out.println(Result.error("密码错误"));
        } catch (LockedAccountException e){
            e.printStackTrace();
            System.out.println(Result.error("错误"));
        }
        System.out.println("登陆成功");
    }

    }


